<?php
namespace Liaiyong\Light\Core;

use Exception;

class Error extends Exception {
    /**
     * 异常错误队列
     */
    public static $stack = array();
    /**
     * 快速实例化对象
     */
    public static function __callStatic($name, $arguments)  {
        global $CFG;

        if(isset($CFG['error']) && isset($CFG['error'][$name])) {
            $code = $CFG['error'][$name];
            $msg = $CFG['error'][$code];
        } else  {
            $code = 999998;
            $msg = 'undefine_error(%s)';
        }
        if($code == 999998) {
            array_unshift($arguments, $name);
        }
        // 组合
        array_unshift($arguments, $msg);

        return new Error(@call_user_func_array('sprintf', $arguments), $code);
    }
    /**
     * 获取最后一次Error
     */
    public static function __lastError() {
        reset(Error::$stack);
        $current = current(Error::$stack);
        return empty($current) ? Error::unkown_error() : $current;
    }
    /**
     * @param string|Errode $message
     * @param int $code
     * @param Exception $previous
     */
    public function __construct($message = "", $code = 0, Exception $previous = NULL) {
        parent::__construct($message, $code, $previous);

        array_unshift(Error::$stack, $this);
    }
}

// PHP END
